import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @author Huhao
 * &#064;date  2024/10/02
 */


public class Form1 extends JFrame {
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenu compileMenu;
    private JButton arithmeticCheck;
    private JMenuItem openMenuItem, saveMenuItem, exitMenuItem;
    private JMenuItem allowEditMenuItem, exitEditMenuItem;
    private JMenuItem lexicalAnalysisMenuItem, automataSystemMenuItem, LL1AnalysisMenuItem, LR0AnalysisMenuItem, codePreprocessingMenuItem, nfaDfaMfaMenuItem, SLR1AmalysisMenuItem;
    private JToolBar toolBar;
    private JButton openButton, saveButton, exitButton, allowEditButton, exitEditButton, lexicalAnalysisButton, automataSystemButton, LL1AnalysisButton, LR0AnalysisButton;
    private JPanel panel;
    private JTextArea sourceCodeArea, tokenSequenceArea, lexicalErrorArea;

    public Form1() {
        setTitle("编译原理实验-22111303050-胡昊");
        setSize(1067, 618);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        initializeComponents();
    }

    private void initializeComponents() {
        menuBar = new JMenuBar();
        fileMenu = new JMenu("文件（F）");
        editMenu = new JMenu("编辑");
        compileMenu = new JMenu("编译");
        arithmeticCheck = new JButton("算术表达式检验");
        arithmeticCheck.setBorderPainted(false);
        arithmeticCheck.setContentAreaFilled(false);
        arithmeticCheck.setFocusPainted(false);


        openMenuItem = new JMenuItem("打开（O）   Ctrl+O");
        saveMenuItem = new JMenuItem("保存（S）    Ctrl+S");
        exitMenuItem = new JMenuItem("退出（X）    Ctrl+X");

        allowEditMenuItem = new JMenuItem("允许编辑");
        exitEditMenuItem = new JMenuItem("退出编辑");

        lexicalAnalysisMenuItem = new JMenuItem("词法分析");
        automataSystemMenuItem = new JMenuItem("自动机系统");
        LL1AnalysisMenuItem = new JMenuItem("LL（1）分析");
        LR0AnalysisMenuItem = new JMenuItem("LR（0）分析");
        codePreprocessingMenuItem = new JMenuItem("代码预处理");
        nfaDfaMfaMenuItem = new JMenuItem("NFA_DFA_MFA");
        SLR1AmalysisMenuItem = new JMenuItem("SLR(1）分析");

        arithmeticCheck.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new AlgorithmCheck();
            }
        });


        openMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
                        sourceCodeArea.setText("");
                        String line;
                        while ((line = reader.readLine()) != null) {
                            sourceCodeArea.append(line + "\n");
                        }
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "无法打开文件");
                    }
                }
            }
        });

        saveMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showSaveDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try (BufferedWriter writer = new BufferedWriter(new FileWriter(selectedFile))) {
                        writer.write(sourceCodeArea.getText());
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "无法保存文件");
                    }
                }
            }
        });

        exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        allowEditMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sourceCodeArea.setEditable(true);
            }
        });

        exitEditMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sourceCodeArea.setEditable(false);
            }
        });

        lexicalAnalysisMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    Lexical lexical = new Lexical();
                    String sourceCode = sourceCodeArea.getText();
                    String result = lexical.Scanner(sourceCode);
                    tokenSequenceArea.setText(result);
                    lexicalErrorArea.setText(lexical.errorDetailInfo);
            }
        });

        automataSystemMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new automataSystem();
            }
        });

        nfaDfaMfaMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new NFA_DFA_MFA();
            }
        });

        LL1AnalysisMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new LL1();
            }
        });

        LR0AnalysisMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new LR0();
            }
        });

        codePreprocessingMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new codePreprocessing();
            }
        });

        SLR1AmalysisMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new SLR1();
            }
        });

        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(exitMenuItem);

        editMenu.add(allowEditMenuItem);
        editMenu.add(exitEditMenuItem);

        compileMenu.add(lexicalAnalysisMenuItem);
        compileMenu.add(automataSystemMenuItem);
        compileMenu.add(LL1AnalysisMenuItem);
        compileMenu.add(LR0AnalysisMenuItem);
        compileMenu.add(codePreprocessingMenuItem);
        compileMenu.add(nfaDfaMfaMenuItem);
        compileMenu.add(SLR1AmalysisMenuItem);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(compileMenu);
        menuBar.add(arithmeticCheck);

        toolBar = new JToolBar();
        openButton = new JButton("打开");
        saveButton = new JButton("保存");
        exitButton = new JButton("退出");
        allowEditButton = new JButton("允许编辑");
        exitEditButton = new JButton("退出编辑");

        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
                        sourceCodeArea.setText("");
                        String line;
                        while ((line = reader.readLine()) != null) {
                            sourceCodeArea.append(line + "\n");
                        }
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "无法打开文件");
                    }
                }
            }
        });

        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showSaveDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try (BufferedWriter writer = new BufferedWriter(new FileWriter(selectedFile))) {
                        writer.write(sourceCodeArea.getText());
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "无法保存文件");
                    }
                }
            }
        });

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });


        allowEditButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sourceCodeArea.setEditable(true);
            }
        });

        exitEditButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                sourceCodeArea.setEditable(false);
            }
        });

        toolBar.add(openButton);
        toolBar.add(saveButton);
        toolBar.add(exitButton);
        toolBar.add(allowEditButton);
        toolBar.add(exitEditButton);

        panel = new JPanel();
        panel.setLayout(new GridLayout(1, 3));

        sourceCodeArea = new JTextArea();
        tokenSequenceArea = new JTextArea();
        lexicalErrorArea = new JTextArea();

        sourceCodeArea.setEditable(false);
        tokenSequenceArea.setEditable(false);
        lexicalErrorArea.setEditable(false);

        panel.add(new JScrollPane(sourceCodeArea));
        panel.add(new JScrollPane(tokenSequenceArea));
        panel.add(new JScrollPane(lexicalErrorArea));

        setJMenuBar(menuBar);
        add(toolBar, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
    }

}
